package com.yxhl.zoume.core.car.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yxhl.zoume.R;
import com.yxhl.zoume.common.adapter.BaseAdapter;
import com.yxhl.zoume.core.car.model.CarSelectPassengerEntity;
import com.yxhl.zoume.core.car.model.CarSelectPassengerFromContactEvent;
import com.yxhl.zoume.data.rxbus.RxBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by sugarya on 16/8/15.
 */
public class CarSelectPassengerAdapter extends BaseAdapter<BaseAdapter.BaseViewHolder> {

    private static final String TAG = "CarSelectPassengerAdapter";

    private static final int LETTER_VIEW_TYPE = 1;


    private Context mContext;
    private List<CarSelectPassengerEntity> mCarSelectPassengerEntityList;
    private List<CarSelectPassengerEntity> mBackUpCarSelectPassengerEntityList;

    public CarSelectPassengerAdapter(Context context, List<CarSelectPassengerEntity> carSelectPassengerEntityList) {
        mContext = context;
        mCarSelectPassengerEntityList = carSelectPassengerEntityList;

    }

    @Override
    public int getItemViewType(int position) {
        if (mCarSelectPassengerEntityList != null) {
            CarSelectPassengerEntity passengerEntity = mCarSelectPassengerEntityList.get(position);
            String displayContent = passengerEntity.getDisplayContent();
            if (!TextUtils.isEmpty(displayContent) && displayContent.matches("[A-Z]{1}")) {
                return LETTER_VIEW_TYPE;
            }
        }
        return super.getItemViewType(position);
    }

    @Override
    public BaseAdapter.BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        switch (viewType) {
            case LETTER_VIEW_TYPE:
                View letterView = layoutInflater.inflate(R.layout.item_bus_station_letter, parent, false);
                return new LetterViewHolder(letterView);
            default:
                View passengerView = layoutInflater.inflate(R.layout.item_car_select_passenger, parent, false);
                return new PassengerHolder(passengerView);
        }
    }


    @Override
    public void onBindViewHolder(BaseAdapter.BaseViewHolder holder, int position) {
        if (holder instanceof LetterViewHolder) {
            LetterViewHolder viewHolder = (LetterViewHolder) holder;
            viewHolder.onBindViewHolder(position);
        } else if (holder instanceof PassengerHolder) {
            PassengerHolder passengerHolder = (PassengerHolder) holder;
            passengerHolder.onBindViewHolder(position);
        }
    }

    @Override
    public int getItemCount() {
        return mCarSelectPassengerEntityList.size();
    }


    /**
     * 第一次更新数据
     */
    public void notifyOriginalData(List<CarSelectPassengerEntity> entityList) {
        if (entityList != null) {
            mBackUpCarSelectPassengerEntityList = new ArrayList<>();
            mBackUpCarSelectPassengerEntityList.addAll(entityList);

            if (mCarSelectPassengerEntityList != null) {
                mCarSelectPassengerEntityList.addAll(entityList);
                notifyItemRangeInserted(0, entityList.size());
            }
        }
    }

    /**
     * 更新数据
     */
    public void notifyData(List<CarSelectPassengerEntity> entityList) {
        if (entityList != null) {
            //数据清空
            if (mCarSelectPassengerEntityList != null && mCarSelectPassengerEntityList.size() > 0) {
                mCarSelectPassengerEntityList.clear();
                notifyDataSetChanged();
            }
            if (mCarSelectPassengerEntityList != null) {
                mCarSelectPassengerEntityList.addAll(entityList);
                notifyItemRangeInserted(0, entityList.size());
            }
        }
    }

    /**
     * 恢复到原来的数据
     */
    public void returnOriginal() {
        if (mCarSelectPassengerEntityList != null) {
            mCarSelectPassengerEntityList.clear();
            notifyDataSetChanged();
        }

        if (mCarSelectPassengerEntityList != null && mBackUpCarSelectPassengerEntityList != null) {
            mCarSelectPassengerEntityList.addAll(mBackUpCarSelectPassengerEntityList);
            notifyItemRangeInserted(0, mBackUpCarSelectPassengerEntityList.size());
        }
    }


    class PassengerHolder extends BaseAdapter.BaseViewHolder {

        @BindView(R.id.ll_item_car_select_root)
        LinearLayout mCarSelectRootLl;

        @BindView(R.id.tv_item_car_select_passenger_name)
        TextView passengerNameTv;

        @BindView(R.id.tv_item_car_select_passenger_mobile)
        TextView passengerMobileTv;

        public PassengerHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @OnClick(R.id.ll_item_car_select_root)
        void onItemClick() {
            int layoutPosition = getLayoutPosition();
            if (layoutPosition >= 0 && layoutPosition < mCarSelectPassengerEntityList.size()) {
                CarSelectPassengerEntity passengerEntity = mCarSelectPassengerEntityList.get(layoutPosition);
                RxBus.getInstance().send(new CarSelectPassengerFromContactEvent(passengerEntity.getPassengerMobile()));
            }
        }

        void onBindViewHolder(int position) {
            if (position > 0 && position < mCarSelectPassengerEntityList.size()) {
                CarSelectPassengerEntity passengerEntity = mCarSelectPassengerEntityList.get(position);
                if (passengerEntity != null) {
                    String displayContent = passengerEntity.getDisplayContent();
                    String passengerMobile = passengerEntity.getPassengerMobile();
                    setDisplayContent(displayContent);
                    setPassengerMobile(passengerMobile);
                }
            }
        }

        void setDisplayContent(String passengerName) {
            if (!TextUtils.isEmpty(passengerName)) {
                passengerNameTv.setText(passengerName);
            }
        }

        void setPassengerMobile(String mobileNumber) {
            if (!TextUtils.isEmpty(mobileNumber)) {
                passengerMobileTv.setText(mobileNumber);
            }
        }

    }


    class LetterViewHolder extends BaseAdapter.BaseViewHolder {

        @BindView(R.id.tv_item_bus_station_letter)
        TextView letterTv;

        public LetterViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        void onBindViewHolder(int position) {
            if (position >= 0 && position < mCarSelectPassengerEntityList.size()) {
                CarSelectPassengerEntity carSelectPassengerEntity = mCarSelectPassengerEntityList.get(position);
                if (carSelectPassengerEntity != null) {
                    String letter = carSelectPassengerEntity.getDisplayContent();
                    if (!TextUtils.isEmpty(letter)) {
                        if (letter.matches("[A-Z]{1}")) {
                            setContent(letter);
                        }
                    }
                }
            }
        }

        void setContent(String str) {
            letterTv.setText(str);
        }
    }


}
